<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/7/27
 * Time: 下午3:36
 */

namespace app\index\service;

use app\index\model\Quota;
class QuotaService
{
    const SUB_CATE_QUOTA_LIST_KEY = "sub_cate_quota_list_key";
    const SUB_CATE_QUOTA_DETAIL_KEY = "sub_cate_quota_detail_key";
    const EXPIRE_IN = 60*60*5;
    /**
     * @param $subcateId
     * 通过二级资质分类id 获取该资质所有等级的的三个报价
     */
    public static function getSubCateQuotaList($subcateId){
        $cache_detail = cache(self::SUB_CATE_QUOTA_LIST_KEY.$subcateId);
        if($cache_detail){
            return $cache_detail;
        }
        $data = Quota::getQuotaListBySubCateId($subcateId);
        cache(self::SUB_CATE_QUOTA_LIST_KEY.$subcateId, $data, self::EXPIRE_IN);
        return $data;
    }
    public static function getQuotaDetail($supcategory_id,$subcategory_id,$level){
        $cache_detail = cache(self::SUB_CATE_QUOTA_DETAIL_KEY.$supcategory_id.$subcategory_id.$level);
        if($cache_detail){
            return $cache_detail;
        }
        $data = Quota::getOneByOptions($supcategory_id,$subcategory_id,$level);
        cache(self::SUB_CATE_QUOTA_DETAIL_KEY.$supcategory_id.$subcategory_id.$level, $data, self::EXPIRE_IN);
        return $data;

    }
    //获取多个详情
    public static function getMutilQuotaDetail($opt){
        $cache_detail = cache(self::SUB_CATE_QUOTA_DETAIL_KEY.$opt);
        if($cache_detail){
            return $cache_detail;
        }
        $data = Quota::getMutilByOptions($opt);
        cache(self::SUB_CATE_QUOTA_DETAIL_KEY.$opt, $data, self::EXPIRE_IN);
        return $data;
    }
    public static function getFeeList($fee){
        $fee_list = [];
        if($fee){
            foreach ($fee as $item){
                $tmp = [];
                switch ($item["type"]){
                    case 0:$type = "人员费用";break;
                    case 1:$type = "服务费用";break;
                    case 2:$type = "业绩费用";break;
                    default:$type = "其他费用";break;
                }
                $tmp["type"]  = $type;
                $tmp["fee"] = floatval($item["fee_price"])*$item["fee_num"];
                $tmp["fee"] = sprintf("%.2f", $tmp["fee"]);
                $flag = 0;
                if($fee_list){
                    foreach ($fee_list as &$fee_item){
                        if($tmp["type"] == $fee_item["type"]){

                            $fee_item["fee"] = $fee_item["fee"]+$tmp["fee"];
                            $fee_item["fee"] = sprintf("%.2f", $fee_item["fee"]);
                            $flag = 1;
                        }
                    }
                }
                if($flag == 0){
                    array_push($fee_list,$tmp);
                }

            }
            $total = 0;
            if($fee_list){
                foreach ($fee_list as $item){
                    $total = $total+$item["fee"];
                }
            }

            $tmp["type"] = "总费用";
            $tmp["fee"] = $total;
            array_push($fee_list,$tmp);
        }

        return $fee_list;

    }
}